package com.etasist;

/**
 * @author issa
 * 
 */

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerAgent implements Runnable {
	ServerSocket s = null;
	public static int port=4444;
	public static ConfigInfo configInfo;
	
	public static void main(String[] args) throws IOException {
		if (args.length < 1) {
			
			System.out.println("Usage: java com.etasist.ServerAgent <configuration file>");
			System.exit(-1);
		}
		
		System.out.println("initializing confoguration");
		configInfo = new ConfigInfo(args[0]);
		port = ConfigInfo.serverPort;
		ServerAgent serverAgent = new ServerAgent(port);
		
		serverAgent.run();
	}

	ServerAgent(int portt) {
		port = portt;
		try {
			s = new ServerSocket(port);
		} catch (IOException e) {
			System.err.println("Could not listen on port: " + port +"\nPort could be in use.");
			System.exit(1);
		}
	}

	public void run() {
		try {
			while (true) {
				Socket incoming = s.accept();
				Runnable r = new ClientInputOutput(incoming);
				Thread t = new Thread(r);
				t.start();
			}
		} catch (IOException e) {
			System.out.println("\nServerAgent.run: " + e);
		}

	}

}